package com.ybfenpan.controller.admin.member;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Daili;
import com.ybfenpan.model.Member;
import com.ybfenpan.service.DailiService;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.ParamWageService;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.ybfenpan.utils.StringUtil.isMobile;


@Controller
public class MemberAddController extends BaseController {

    @Resource
    private MemberService memberService;
    @Resource
    private ParamWageService paramWageService;
    @Resource
    private DailiService dailiService;

    @RequestMapping("admin/member/memberAddShow.shtml")
    public String show(HttpServletRequest request) throws Exception {

        //获取list
        Map formMap = getParameterMap(request);
        List dailiList = dailiService.queryForList("select", formMap);
        request.setAttribute("dailiList", dailiList);

        //接点图传过来的参数
        String fatherLoginName = MapUtil.getString(formMap, "fatherLoginName");
        String qu = MapUtil.getString(formMap, "qu");

        request.setAttribute("qu", qu);
        request.setAttribute("fatherLoginName", fatherLoginName);

        return "admin/member/memberAdd";
    }

    @PostMapping("admin/member/memberAdd.shtml")
    public String add(ModelMap model, HttpServletRequest request) throws Exception {
        Map formMap = getParameterMap(request);
        /*****************验证******************/
        String loginName = MapUtil.getString(formMap, "loginName");
        boolean result = true;
        //账号
        if (loginName==null||"".equals(loginName)){
            setMessage(model, "请输入代理商账号！");
            result = false;
        }

        if (!isMobile(loginName)) {
            setMessage(model, "您输入的账号不是手机号！");
            result = false;

        }

        if (memberService.executMemberByLoginName(loginName) != null) {
            setMessage(model, "该代理商账号已经存在。");
            result = false;

        }

        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(loginName);
        if (matcher.find()) {
            setMessage(model, "登录名不能包含中文。");
            result = false;

        }

        //过滤登录名的特殊字符
        if (loginName.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() != 0) {
            setMessage(model, "该登录名含有特殊字符，请重新填写！");
            result = false;
        }

        if (loginName.length() > loginName.replaceAll(" ", "").length()) {
            setMessage(model, "该登录名含有空格，请重新填写！");
            result = false;
        }

        //推荐人
        String recommendLoginName = MapUtil.getString(formMap, "recommendLoginName");

        Integer recommendId = null;
        Member recommend;
        if (recommendLoginName != null && !recommendLoginName.equals("")) {
            recommend = memberService.executMemberByLoginName(recommendLoginName);
            if (recommend != null) {

                if (recommend.getAccountActive().equals("F")) {
                    setMessage(model, "推荐人未报单。");
                    result = false;
                } else {
                    recommendId = recommend.getId();
                }
            } else {
                setMessage(model, "推荐人不存在。");
                result = false;


            }
        }

        String onePsw = MapUtil.getString(formMap, "onePassword");
        String onePassword = CryptUtil.getInstance().encrypt2String(onePsw, CryptUtil.keyStr);
        if (onePsw.length() < 1) {
            setMessage(model, "请输入登陆密码");
            result = false;
        }

        String twoPsw = MapUtil.getString(formMap, "twoPassword");
        String twoPassword = CryptUtil.getInstance().encrypt2String(twoPsw, CryptUtil.keyStr);
        if (twoPsw.length() < 1) {
            setMessage(model, "请输入安全密码");
            result = false;
        }

        if (!result) {
            List dailiList = dailiService.queryForList("select", formMap);
            request.setAttribute("dailiList", dailiList);
            request.setAttribute("member", formMap);
            return "admin/member/memberAdd";
        }

        Member member = new Member();

        member.setLoginName(loginName);
        member.setOnePassword(onePassword);
        member.setTwoPassword(twoPassword);
        member.setRecommendId(recommendId);


        member.setCardNo(MapUtil.getString(formMap, "cardNo"));
        member.setRemark(MapUtil.getString(formMap, "remark"));
        member.setWei(MapUtil.getString(formMap, "wei"));
        member.setQq(MapUtil.getString(formMap, "qq"));
        member.setTel(MapUtil.getString(formMap, "tel"));
        member.setPersonName(MapUtil.getString(formMap, "personName"));

//		member.setBankShi(MapUtil.getString(formMap, "bankShi"));
        member.setBankName(MapUtil.getString(formMap, "bankName"));
        member.setBankAddress(MapUtil.getString(formMap, "bankAddress"));
        member.setBankCardNo(MapUtil.getString(formMap, "bankCardNo"));
//		member.setBankSheng(MapUtil.getString(formMap, "bankSheng"));
        member.setBankPersonName(MapUtil.getString(formMap, "personName"));
        memberService.saveOne(member);
        return "redirect:memberNoActiveList.shtml";

    }

    @RequestMapping("admin/member/ajaxGetRecommendByName.shtml")
    @ResponseBody
    public void ajaxTuRecommendAction(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("*********ajaxGetRecommendByName.shtml***********");
        Map formMap = getParameterMap(request);
        String recommendLoginName = MapUtil.getString(formMap, "recommendLoginName");
        Member member = memberService.executMemberByLoginName(recommendLoginName);
        String personName;
        HashMap resultformMap = new HashMap();
        if (member != null) {

            personName = member.getPersonName();
            resultformMap.put("member", member);
            resultformMap.put("personName", personName);


            renderSuccess(response, true, resultformMap);
        } else {

            renderSuccess(response, false, resultformMap);

        }
    }


    @RequestMapping("admin/member/ajaxGetFatherByName.shtml")
    @ResponseBody
    public void ajaxTuFahterAction(HttpServletRequest request, HttpServletResponse response) {

        Object retObj = null;
        Map formMap = getParameterMap(request);
        String fatherLoginName = MapUtil.getString(formMap, "fatherLoginName");
        Member member = memberService.executMemberByLoginName(fatherLoginName);
        String personName = "";
        HashMap resultformMap = new HashMap();
        if (member != null) {
            retObj = member;
            if (member.getZuoId() != null && member.getYouId() != null) {

                resultformMap.put("title", "姓名：" + member.getPersonName() + "，左右区均有人，请重新填写。");
            }

            if (member.getZuoId() != null && member.getYouId() == null) {

                resultformMap.put("title", "姓名：" + member.getPersonName() + "，左区有人，请放在右区。");
            }
            if (member.getZuoId() == null && member.getYouId() != null) {

                resultformMap.put("title", "姓名：" + member.getPersonName() + "，右区有人，请放在左区。");
            }
            if (member.getZuoId() == null && member.getYouId() == null) {

                resultformMap.put("title", "姓名：" + member.getPersonName() + "，左右区都没有人。");
            }

            renderSuccess(response, true, resultformMap);
        } else {

            renderSuccess(response, false, resultformMap);

        }
    }

    @RequestMapping("admin/member/ajaxGetCenterByName.shtml")
    @ResponseBody
    public void ajaxTuCenterAction(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("****1011111111******:");
        Object retObj = null;
        Map formMap = getParameterMap(request);
        String centerLoginName = MapUtil.getString(formMap, "centerLoginName");
        System.out.println("**********:" + centerLoginName);
        Member member = memberService.executMemberByLoginName(centerLoginName);
        String personName = "";
        HashMap resultformMap = new HashMap();
        if (member != null) {
            retObj = member;
            if (member.getAccountCenter().equals("F")){
                resultformMap.put("title", "姓名：" + member.getPersonName() + "，该代理商不是服务中心！");
            }else {
                resultformMap.put("title", "服务中心姓名：" + member.getPersonName());
            }
           /* personName = member.getPersonName();
            resultformMap.put("member", member);
            resultformMap.put("personName", personName);
*/
            renderSuccess(response, true, resultformMap);
        } else {


            renderSuccess(response, false, resultformMap);

        }
    }


    /**
     * loginName检测  ajax
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("admin/member/ajaxGetMemberByName.shtml")
    @ResponseBody
    public void ajaxGetMemberByName(HttpServletRequest request, HttpServletResponse response) {


        Map formMap = getParameterMap(request);
        String loginName = MapUtil.getString(formMap, "loginName");
        Member member = memberService.executMemberByLoginName(loginName);

        HashMap resultformMap = new HashMap();

        if (member == null) {

            renderSuccess(response, true, resultformMap);
        } else {


            renderSuccess(response, false, resultformMap);

        }

    }

    @RequestMapping("admin/member/ajaxGetMemberListByName.shtml")
    @ResponseBody
    public void ajaxGetMemberListByName(HttpServletRequest request, HttpServletResponse response) {


        Map formMap = getParameterMap(request);
        String loginName = MapUtil.getString(formMap, "loginName");
        Member member = memberService.executMemberByLoginName(loginName);

        Map<String, Object> memberMap = new HashMap<String, Object>();

        List<Member> list = this.memberService.queryForList("select", memberMap);

        if (member == null) {

            renderSuccess(response, true, list);
        } else {


            renderSuccess(response, false, list);

        }

    }

}
